package com.qianyu.project.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.ApplyProgressPayment;
import com.qianyu.project.entity.ApplyWorkList;
import com.qianyu.project.mapper.ApplyProgressPaymentMapper;
import com.qianyu.project.service.IApplyProgressPaymentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.project.service.IApplyWorkListService;
import com.qianyu.project.utils.PageUtil;
import com.qianyu.project.vo.ProgressPaymentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 进度款申报 服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-01-30
 */
@Service
public class ApplyProgressPaymentServiceImpl extends ServiceImpl<ApplyProgressPaymentMapper, ApplyProgressPayment> implements IApplyProgressPaymentService {

    @Autowired
    private ApplyProgressPaymentMapper applyProgressPaymentMapper;
    @Autowired
    private IApplyWorkListService applyWorkListService;

    @Override
    public String getMaxId() {
        String code = applyProgressPaymentMapper.getMaxId();
        if(code == null){
            code = DateUtil.today().replaceAll("-", "") + "000";
        }
        return code;
    }

    @Transactional
    @FlowBegin("apply_progress_payment")
    @Override
    public Integer saveBoth(@FlowEntry("id") ApplyProgressPayment applyForm, List<ApplyWorkList> applyDetails) {
        applyForm.setApproveStatus(1);
        applyProgressPaymentMapper.insert(applyForm);
        applyWorkListService.saveBatch(applyDetails);
        return 1;
    }

    @Override
    public IPage<ProgressPaymentVo> getList(Map<String, String> params) {
        return applyProgressPaymentMapper.getList(PageUtil.createPage(params), params);
    }

    @Override
    public ProgressPaymentVo selectOne(String id) {
        return applyProgressPaymentMapper.getOne(id);
    }
}
